package com.gx.admin.service;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import javax.validation.Valid;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.student.demo.entity.Course;
import com.student.demo.repository.CourseRepository;

@Service
public class CourseService {
	
	@Resource
	private CourseRepository courseRepository;

	@Transactional
	public Course add(Course course) {
		return courseRepository.save(course);
	}

	public List<Course> findByPage(Integer page, int i) {
		Sort sort = new Sort(Direction.ASC,"dNumber");
		PageRequest p = new PageRequest(page,i,sort);
		 Page<Course> findAll = courseRepository.findAll(p);
		return findAll.getContent();
	}

	@Transactional
	public void remove(Integer id) {
		courseRepository.deleteById(id);
	}
	
	public Course findOne(Integer id) {
		return courseRepository.findBydId(id);
	}

	public List<Course> findBydName(String name) {
		return courseRepository.findByDName(name);
	}

	
}
